package com.syedu.day18;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author Administrator
 * @Description 自定义注解测试
 * @createTime 2022/4/11 13:33
 */
@MyAnnotation
public class TestMyAnnotation {

    //@MyAnnotation
    private int myField;

    //@MyAnnotation
    public TestMyAnnotation() {

    }

    //@MyAnnotation(value="abc")
    //@MyAnnotation("abc")
    @MyAnnotation(value = "abc", nums = {10, 20, 30})
    public void method() {

    }

    public static void main(String[] args) {
        /**
         * 获取注解,以及获取注解中的属性值 -- 原理:反射技术
         */
        try {
            //1.获取类的Class实例
            Class c = TestMyAnnotation.class;
            //2.获取method方法
            Method method = c.getMethod("method");
            //3.从方法上拿到注解
            //注意: 注解持久力必须设置为RUNTIME才可以反射获取
            //MyAnnotation annotation = (MyAnnotation) c.getAnnotation(MyAnnotation.class);
            MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
            //4.从注解对象中获取属性值
            System.out.println(annotation.value());
            System.out.println(Arrays.toString(annotation.nums()));
            //TODO...
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
